;
! function() {
	var layer = layui.layer,
		laypage = layui.laypage;

	layer.load(2);

	to_page(1, 10);

	// 请求分页数据
	function to_page(pageNum, pageSize) {
		$.ajax({
			url: "/bizjobstype/",
			type: "post",
			data: {
				"pageNum": pageNum,
				"pageSize": pageSize
			},
			async: false,
			success: function(result) {
				layer.closeAll('loading');
				if(result.totalRow == 0) {
					// 提示没有数据
					$("#content").html("<tr><td colspan='4' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
					// 隐藏分页条
					$("#page_info").hide();
				} else {
					// 拼装tr并赋给页面
					build_table(result);
					// 拼装分页总览信息并赋给页面
					build_pageInfo(result);
					// 显示分页按钮
					laypage.render({
						elem: 'page_nav_div',
						count: result.totalRow,
						curr: result.pageNumber,
						limit: result.pageSize,
						theme: "#FF6700",
						prev: "<em>«</em>",
						next: "<em>»</em>",
						layout: ['prev', 'page', 'next', 'limit', 'skip'],
						jump: function(obj, first) {
							if(!first) {
								to_page(obj.curr, obj.limit);
							}
						}
					});
					// 显示分页条
					$("#page_info").show();
				}
			}
		});
	}

	// 构建分页细节
	function build_pageInfo(pageInfo) {
		$("#pagination_overview").html(
			"当前第" + pageInfo.pageNumber + "页," +
			"共有" + pageInfo.totalPage +
			"页,总计" +
			pageInfo.totalRow + "条记录");
	}

	// 构建表格
	function build_table(page) {
		// 清空tbody
		$("#content").empty();
		// 遍历数据并累加
		$.each(page.list, function() {
			var nameTd = $("<td></td>").append(this.name);
			var updateTimeTd = $("<td></td>").append(getMyDate(this.updateTime));
			var operation = $("<td>" +
				"<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-danger del-bjt'>" +
				"<i class='layui-icon'>&#xe640;</i>删除</button>" +
				"</td>");
			// 赋给tbody
			$("<tr id='" + this.id + "'></tr>").append(nameTd).append(updateTimeTd).append(operation).appendTo("#content");
		});
	}

	//删除任务类型
	$(document).on("click", ".del-bjt", function() {
		var id = $(this).parent().parent().attr("id");
		layer.confirm("确定要删除吗?", {
			icon: 3,
			title: '温馨提示'
		}, function(index) {
			$.ajax({
				url: "/bizjobstype/delBizJobsType",
				type: "post",
				data: {
					"id": id
				},
				success: function(result) {
					layer.msg(result.msg);
					if(result.status == 0) {
						to_page(1, 10);
					}
				}
			});
			layer.close(index);
		});
	});

	//添加任务类型
	$(document).on("click", ".add-bjt", function() {
		layer.prompt({
			title: '添加任务类型名称',
			formType: 2
		}, function(text, index) {
			layer.close(index);
			$.ajax({
				url: "/bizjobstype/addBizJobsType",
				type: "post",
				data: {
					"name": text
				},
				success: function(result) {
					layer.msg(result.msg);
					if(result.status == 0) {
						to_page(1, 10);
					}
				}
			});
			layer.close(index);
		});
	});

	function getMyDate(str) {
		var oDate = new Date(str),
			oYear = oDate.getFullYear(),
			oMonth = oDate.getMonth() + 1,
			oDay = oDate.getDate(),
			oHour = oDate.getHours(),
			oMin = oDate.getMinutes(),
			oSen = oDate.getSeconds(),
			oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen); //最后拼接时间  
		return oTime;
	};
	//补0操作
	function getzf(num) {
		if(parseInt(num) < 10) {
			num = '0' + num;
		}
		return num;
	}
}();